package com.sx.pipeline.ui.supply

import android.app.Application
import android.text.TextUtils
import androidx.databinding.ObservableField
import com.alibaba.android.arouter.launcher.ARouter
import com.amap.api.services.core.PoiItem
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.bus.RxBus
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.Logs
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.R
import com.sx.pipeline.app.event.BusEvent
import com.sx.pipeline.app.event.DeviceEvent
import com.sx.pipeline.app.event.EventTypes
import com.sx.pipeline.app.event.RefreshEvent
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.repos.http.req.EquipmentAddDTO
import com.sx.pipeline.repos.http.req.SupplyOrderAddDTO
import com.sx.pipeline.repos.http.req.TaskAddDTO
import com.sx.pipeline.repos.http.res.*
import com.sx.pipeline.ui.task.entity.ReleaseTaskListVO
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers

/**
 * @Author: YY
 * @Date: 2022/4/13
 * @Description: TODO
 */
class ReleaseSupplyVM(app: Application) : CYBaseViewModel(app) {
    // 省ID
    val provinceIDField: ObservableField<Int> = ObservableField()

    // 市ID
    val cityIDField: ObservableField<Int> = ObservableField()

    // 区ID
    val areaIDField: ObservableField<Int> = ObservableField()

    // 地址详情
    val addressField: ObservableField<String> = ObservableField()

    // 经度
    val longitudeField: ObservableField<String> = ObservableField()

    // 纬度
    val latitudeField: ObservableField<String> = ObservableField()

    /**
     * 获取我发布的任务类型标题
     */
    fun getSupplyTabsList(): List<String> {
        val tabs = mutableListOf<String>()
        tabs.add(getApplication<Application>().getString(R.string.txt_pending_review))
        tabs.add(getApplication<Application>().getString(R.string.txt_be_inquired))
        tabs.add(getApplication<Application>().getString(R.string.txt_inquired))
        tabs.add(getApplication<Application>().getString(R.string.txt_cancelled))
        tabs.add(getApplication<Application>().getString(R.string.txt_expired))
        tabs.add(getApplication<Application>().getString(R.string.txt_rejected))
        return tabs
    }


    fun getSupplyTaskList(): MutableList<ReleaseTaskListVO> {
        val releaseList = mutableListOf<ReleaseTaskListVO>()

        val device1 = ReleaseTaskListVO("1", 1)
        val device2 = ReleaseTaskListVO("2", 2)
        val device3 = ReleaseTaskListVO("3", 3)
        val device4 = ReleaseTaskListVO("4", 4)
        val device5 = ReleaseTaskListVO("5", 5)
        val device6 = ReleaseTaskListVO("6", 6)


        releaseList.add(device1)
        releaseList.add(device2)
        releaseList.add(device3)
        releaseList.add(device4)
        releaseList.add(device5)
        releaseList.add(device6)

        return releaseList
    }

    /**
     * 获取供应配置数据
     */
    val taskListEvent: SingleLiveEvent<TaskConfigVo> = SingleLiveEvent()

    //供应标题
    val supplyTitleData: MutableList<String> = ArrayList()

    //计量单位
    val supplyUnitData: MutableList<String> = ArrayList()

    //结算方式
    val settleTypeData: MutableList<String> = ArrayList()

    fun getSupply() {
        addSubscribe(
            mModel.getSupply()
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<TaskConfigVo, BaseViewModel<*>>
                    (this, true) {

                    override fun onResult(result: TaskConfigVo) {
                        taskListEvent.postValue(result)
                        for (data in result.SUPPLY_TITLE) {
                            supplyTitleData?.add(data.name)
                        }
                        for (data in result.UNIT_OF_MEASUREMENT) {
                            supplyUnitData?.add(data.name)
                        }
                        for (data in result.SETTLEMENT_METHOD) {
                            settleTypeData?.add(data.name)
                        }

                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort("$msg")

                    }
                })
        )
    }


    /**
     * 发布供应
     */
    fun supplyOrderAdd(
        serverId: Int, //服务id
        title: String, //供应标题
        supplyDescribe: String,//供应描述
        startTime: String, //开始时间
        endTime: String, //结束时间
        contact: String,
        contactPhone: String,
        paymentMethod: String,//结算方式
        annexUrl: String, //附件
        payPassWord: String, //支付密码
    ) {

        val dto = SupplyOrderAddDTO(
            serverId,
            title,
            supplyDescribe,
            startTime,
            endTime,
            contact,
            contactPhone,
            paymentMethod,
            annexUrl,
            payPassWord
        )
        addSubscribe(
            mModel.supplyOrderAdd(dto)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {

                    override fun onResult(result: Any) {
                        ToastUtils.showShort("发布成功")
                        ARouter.getInstance().build(RoutePath.Mine.RELEASE_RESULT).navigation()
                        finish()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort("$msg")
                    }
                })
        )
    }


    /**
     * 获取供应发布列表
     */
    var page = 1
    val releaseSupplyListEvent: SingleLiveEvent<List<ReleaseSupplyVo>> = SingleLiveEvent()
    fun pushSupplyPage(status: Int) {
        addSubscribe(
            mModel.pushSupplyPage(status, page)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Ipage<ReleaseSupplyVo>, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Ipage<ReleaseSupplyVo>) {
                        releaseSupplyListEvent.postValue(result.records)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

    /**
     * 获取供应详情
     */
    val supplyDetailEvent: SingleLiveEvent<SupplyDetailVo> = SingleLiveEvent()
    fun getSupplyOrderDetail(id: Int) {
        addSubscribe(
            mModel.getSupplyOrderDetail(id)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<SupplyDetailVo, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: SupplyDetailVo) {
                        supplyDetailEvent.postValue(result)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }


    /**
     * 询价列表
     */
    val viewInquiryListEvent: SingleLiveEvent<List<ViewInquiryVo>> = SingleLiveEvent()
    fun getInquiryList(orderId: Int) {
        addSubscribe(
            mModel.getInquiryList(orderId)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<List<ViewInquiryVo>, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: List<ViewInquiryVo>) {
                        viewInquiryListEvent.postValue(result)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

    /**
     * 取消供应
     */
    val successSupplyEvent: SingleLiveEvent<Boolean> = SingleLiveEvent()
    fun cancelSupplyOrder(id: Int) {
        addSubscribe(
            mModel.cancelSupplyOrder(id)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Any) {
                        ToastUtils.showShort("取消成功")
                        successSupplyEvent.postValue(true)
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

    /**
     * 删除任务
     */
    fun deleteSupplyOrder(id: Int) {
        addSubscribe(
            mModel.deleteSupplyOrder(id)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Any) {
                        ToastUtils.showShort("删除成功")
                        successSupplyEvent.postValue(true)
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

    /**
     * 发布供应  计算价格
     */
    val supplyPriceEvent: SingleLiveEvent<Double> = SingleLiveEvent()
    fun getSupplyPrice(serviceId: Int, startTime: String, endTime: String) {
        if (serviceId == 0) {
            ToastUtils.showShort("请选择供应标题")
            return
        }
        addSubscribe(
            mModel.getSupplyPrice(serviceId, startTime, endTime)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Double, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Double) {
                        supplyPriceEvent.postValue(result)
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }
}